/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : app_freertos.c
  * Description        : FreeRTOS applicative file
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "app_freertos.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "bsp_sht20.h"
#include "usart.h"
#include "string.h"
#include "cJSON.h"
#include "jansson.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .priority = (osPriority_t) osPriorityNormal,
  .stack_size = 512 * 4
};
/* Definitions for TmHum */
osThreadId_t TmHumHandle;
const osThreadAttr_t TmHum_attributes = {
  .name = "TmHum",
  .priority = (osPriority_t) osPriorityLow,
  .stack_size = 512 * 4
};
/* Definitions for Fan */
osThreadId_t FanHandle;
const osThreadAttr_t Fan_attributes = {
  .name = "Fan",
  .priority = (osPriority_t) osPriorityLow1,
  .stack_size = 512 * 4
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* creation of TmHum */
  TmHumHandle = osThreadNew(StartTask02, NULL, &TmHum_attributes);

  /* creation of Fan */
  FanHandle = osThreadNew(StartTask03, NULL, &Fan_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}
/* USER CODE BEGIN Header_StartDefaultTask */
/**
* @brief Function implementing the defaultTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN defaultTask */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END defaultTask */
}

/* USER CODE BEGIN Header_StartTask02 */
/**
* @brief Function implementing the HuTm thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask02 */
void StartTask02(void *argument)
{
  /* USER CODE BEGIN TmHum */
  /* Infinite loop */
  extern uint8_t  USART1_RxBuff[1024];
  extern volatile uint8_t USART1_RxCounter;	
  BSP_SHT20_GetData();
  for(;;)
  {
	BSP_SHT20_GetData();
    osDelay(500);
  }
  /* USER CODE END TmHum */
}

/* USER CODE BEGIN Header_StartTask03 */
/**
* @brief Function implementing the Fan thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask03 */
void StartTask03(void *argument)
{
  /* USER CODE BEGIN Fan */
  cJSON * data_object=NULL;
  extern  uint8_t USART5_RxBuff[1024];
  char data[1024]={0};
  extern volatile uint8_t USART1_RxCounter;
  extern int  Connect_flag ;
  int key = 0 ;
  int value = 0;
  /* Infinite loop */	
  for(;;)
  {
//	if(Connect_flag ==1){
	USART_printf(&huart1,USART5_RxBuff);		  
	memcpy(data,USART5_RxBuff,1024);
	sscanf(data,"key=%d,val=%d",&key,&value);
	  if(key==304){
		  if(value==1)
			  {
					HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,1);
			  }
		  else if(value==0)
			  {
					HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,0);
		  
			  }  
	  }else if(key ==303){
		 if(value==1)
			  {
					HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,1);
			  }
		  else if(value==0)
			  {
					HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,0);
		  
			  }  
	  }else if(key ==305){
		 if(value==1)
			  {
					HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,1);
			  }
		  else if(value==0)
			  {
					HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,0);
		  
			  }  
		  }
	  else if(key ==307){
		 if(value==1)
			  {
					HAL_GPIO_WritePin(GPIOA,GPIO_PIN_15,1);
			  }
		  else if(value==0)
			  {
					HAL_GPIO_WritePin(GPIOA,GPIO_PIN_15,0);
		  
			  }  
		  }
	  else if(key ==306){
		 if(value==1)
			  {
					HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,1);
			  }
		  else if(value==0)
			  {
					HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,0);
		  
			  }  
		  }
	  

	memset(USART5_RxBuff,0,1024);
	  
    osDelay(500);
  }
  /* USER CODE END Fan */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

